package com.zhiwang.planet.mvvm.viewmodel.my

import android.app.Application
import android.view.View
import androidx.databinding.ObservableField
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import com.zhiwang.planet.BuildConfig
import com.zhiwang.planet.mvvm.model.entity.UserDao
import com.zhiwang.planet.mvvm.model.entity.main.UpdateCheck
import com.zhiwang.planet.util.AppHelper
import javax.inject.Inject

class AccountViewModel @Inject constructor(val app: Application, val userDao: UserDao, val repo: AccountRepository) : AndroidViewModel(app) {

    //交易密码是否绑定过
//    val binded =
//            userDao.users()
//                    .map { it.firstOrNull() }
//                    .map { TextUtils.isEmpty(it?.phone).not() }


//    val bindButtonActionText =
//            binded
//                    .map { bind ->
//                        app.getString(
//                                if (bind) R.string.modify_pay_pwd
//                                else R.string.bind_trade_pwd)
//                    }

    val versionName = ObservableField(

            if (AppHelper.isDev())
                if (AppHelper.testEnv())
                    app.packageManager.getPackageInfo(app.packageName, 0).versionName + "(内网环境测试版)"
                else app.packageManager.getPackageInfo(app.packageName, 0).versionName + "(线上环境测试版)"
            else app.packageManager.getPackageInfo(app.packageName, 0).versionName
    )

//    val phone =
//            userDao
//                    .users()
//                    .map { it.firstOrNull() }
//                    .map {
//                        "ID:"+it?.userId
//                    }

    private val updateCheck: MutableLiveData<UpdateCheck> get() = repo.checkUpdate()

    val newVersionIndicatorVisibility = updateCheck.map {
        return@map if (it.version > BuildConfig.VERSION_CODE)
            View.VISIBLE
        else View.GONE
    }


    var hasLogout = MutableLiveData<Boolean>()

    val err = MutableLiveData<Throwable>()

    fun logout() {
        repo.logout(hasLogout, err)
    }


    fun checkupdate() {

    }
}